import { reactive } from "vue";
import { merge } from "lodash-es";
import type { PullRefreshProps, ListProps } from "vant";

interface RespaonseInter<T = any> {
  total: number;
  list: T[];
  pageNumber: number;
  pageSize: number;
}
interface configInter<T = any> {
  getListApi?: (params: Recordable) => Promise<IResponse<RespaonseInter<T>>>;
  pullObjs?: PullRefreshProps;
  loadObjs?: ListProps;
  defaultParams?: Recordable;
  response: {
    list?: string;
    total?: string;
  };
}

export function usePullDowm<T = any>(config: configInter<T>) {
  const configObjs = reactive({
    // 下拉加载
    pullLoading: false,
    // 上拉加载
    listLoading: false,
    // 是否加载错误
    listError: false,
    // 下拉属性集
    pullObjs: merge(config.pullObjs) as PullRefreshProps,
    // 上拉属性集
    loadObjs: merge({ finished: false }, config.loadObjs),
    // 列表总数
    listTotal: null as null | number,
    // 列表数据
    listData: [] as any[],
    // 请求参数
    params: merge({ pageNum: 1, pageSize: 10 }, config.defaultParams),
    // 返回数组字段值 数组集合字段，总数字段
    response: merge(
      {
        list: "list",
        total: "total"
      },
      config.response
    )
  });

  const methods = {
    onLoad: function () {
      // console.log(configObjs.listLoading);
      configObjs.listLoading = true;
      configObjs.listError = false;
      // 异步更新数据
      setTimeout(() => {
        config.getListApi &&
          config
            .getListApi(configObjs.params)
            .then(res => {
              if (res.data) {
                configObjs.listData = configObjs.listData?.concat(res.data[configObjs.response.list]);
                configObjs.listTotal = res.data[configObjs.response.total];

                configObjs.listLoading = false;
                configObjs.pullLoading = false;
                configObjs.listError = false;

                // 如果返回列表的个数大于总列表个数 则结束加载
                if (configObjs.listData.length >= (configObjs.listTotal as unknown as number)) {
                  configObjs.loadObjs.finished = true;
                } else {
                  configObjs.params.pageNum++;
                }
              } else {
                configObjs.listLoading = false;
                configObjs.pullLoading = false;
                configObjs.listError = false;
                configObjs.loadObjs.finished = true;
              }
            })
            .catch(() => {
              configObjs.listLoading = false;
              configObjs.pullLoading = false;
              // configObjs.loadObjs.finished = true;
              // showToast("加载错误，请刷新重新加载");
              configObjs.listError = true;
              console.log(configObjs, "加载错误");
            });
      }, 700);
    },

    resetData: function () {
      configObjs.listTotal = null;
      configObjs.params.pageNum = 1;
      configObjs.listData = [];
      configObjs.loadObjs.finished = false;
      configObjs.listLoading = false;
      configObjs.pullLoading = configObjs.listTotal === 0 ? true : false;
    },

    onRefresh: function () {
      methods.resetData();

      methods.onLoad();
    }
  };

  return {
    configObjs,
    methods
  };
}
